package com.huaao.micropolice.core.repository.system;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.common.page.Sort;
import com.huaao.micropolice.core.model.system.Dept;
import com.huaao.micropolice.core.model.system.Role;
import com.huaao.micropolice.core.model.system.User;
import com.huaao.micropolice.core.repository.BaseDao;

@Repository
public class RoleDao extends BaseDao{

	public void list(Sort sort, Page page) {
		findByPage(Role.class, null, sort, page);
	}

	public Role load(String id) {
		return (Role)load(Role.class,id);
	}

	public void deleteById(String[] ids) {
		for(String id : ids){
			deleteById(Role.class, id);
		}
	}
	
	public List<Role> findByDept(String deptId){
		return getSession().createQuery("FROM Role where dept.id = ?").setString(0, deptId).list();
	}

	public Set<Role> getAllRoleByUser(User user) {
		Set<Role> roleAll = new HashSet<Role>();
		Dept dept = user.getDept();
		List<Role> deptRoles = findByDept(dept.getId());
		roleAll.addAll(deptRoles);
		if(user.getJobs() != null){
			roleAll.addAll(user.getJobs().getRoles());
		}
		roleAll.addAll(user.getRoles());
		return roleAll;
	}

}
